package com.example.administrator.pomelostreet.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.example.administrator.pomelostreet.MainActivity;
import com.example.administrator.pomelostreet.R;
import com.example.administrator.pomelostreet.pinpai.activity.PinPaiTwoActivity;

import java.io.IOException;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class YouBiGou extends Activity {
    private RecyclerView recyclerView;
    private MAdapter adapter;
    private static String url="http://api.youzibuy.com/tae_coin?myuid=130403735&tbuid=&device_id=8625" +
            "47034764826&platform=android&v=2.1.0&imei=862547034764826&bundleid=186&mode=0&app_id=7&v_" +
            "auth=3.1H1TOP05wx1TKqYm54t6VcDKXH%2BmSMGyU2gIrfw9jvA%3D&statinfo=eyJzb3VyY2UiOiItPiIsImNoY" +
            "W5uZWxpZCI6IjE4NiIsIm9zIjoiMiIsIm90Ijoi5Lit5Zu96IGU6YCaIiwiYXBuIjoiNCIsInVhIjoiTTUgTm90ZSIs" +
            "Im9zdmVyc2lvbiI6IjYuMCIsInNka3ZlcnNpb24iOjIzLCJ2IjoiMi4xLjAiLCJidWlsZHYiOiIyLjEuMCIsInVpZCI" +
            "6MTMwNDAzNzM1LCJvcGVudWRpZCI6ImU3MjRlNDg1LTk0MjMtNDc4NS1iMWQxLWM2NjdlNmM0NmJmMSIsIm1hYyI6I" +
            "jg2MjU0NzAzNDc2NDgyNiIsImFuZHJvaWRpZCI6IjM5MDE0MjE4YTc2NzJmNmYiLCJpbWVpIjoiODYyNTQ3MDM0Nz" +
            "Y0ODI2IiwiaW1zaSI6IjQ2MDAxMzU5MTMzMTIxNyJ9&myclient=0730210018600000&sdkversion=23&path=&" +
            "timestamp=&platform=android&v=2.1.0&device_id=862547034764826";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_you_bi_gou);
        initData();

        recyclerView = (RecyclerView) findViewById(R.id.youbigou_recylerView);

    }
    //初始化RecyclerView中每个item的数据
    private void initData(){
        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                OkHttpClient client =new OkHttpClient();
                Request request =new Request.Builder().get().url(url).build();
                try {
                    Response response = client.newCall(request).execute();
                    if (response.isSuccessful()){
                        String json = response.body().string();
                        if (!TextUtils.isEmpty(json)) {
                            subscriber.onNext(json);
                            subscriber.onCompleted();
                        } else {
                            subscriber.onError(new Throwable("json为空!"));
                            Log.i("================", "json空: "+json);
                        }
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                    subscriber.onError(e);
                }
            }
        }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                        Log.i("=====================", "下载成功");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("============", "异常");
                    }

                    @Override
                    public void onNext(String s) {
//                        String json =s;
//                        Log.i("================", "onCreate: "+json);
                        final Datas datas = OkhttpUtil.parseJson(s);
//                        List<Items> itemses = datas.getItemses();
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(YouBiGou.this);
                        recyclerView.setLayoutManager(linearLayoutManager);
                        adapter = new MAdapter(datas,YouBiGou.this);
                        recyclerView.setAdapter(adapter);
                        setHeaderView(recyclerView);
                        adapter.setOnItemClickListener(new MAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {
                                Integer id=datas.getItemses().get(position).getId();

                                Intent intent =new Intent(YouBiGou.this, PinPaiTwoActivity.class);
                                intent.putExtra("url",id );
                                startActivity(intent);
                            }
                        });

                    }
                });

    }
    private void setHeaderView(RecyclerView view){
        View header = LayoutInflater.from(this).inflate(R.layout.youbigou_header, view, false);
        adapter.setHeaderView(header);
    }

}


